using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Security.Cryptography;
namespace FileManagement
{
    public class ConvertType
    {
        /// <summary>
        ///Function covertt string type bool become int
        /// </summary>
        /// <param name="?"> true to 1</param>
        public static int ConverStringToInt(string str)
        {
            if (str == "true" || str == "True")
                return 1;
            else
                if (str == "false" || str == "False")
                    return 0;
            return -1;
        }
        public static bool ConverStringToBool(string str)
        {
            return ConverIntToBool(ConverStringToInt(str));
        }
        public static bool TestString(string str)
        {
            if ((str == "true" || str == "True") || (str == "false" || str == "False"))
                return true;
            return false;
        }
        public static int ConverStringToInt1(string str)
        {
            if (str == "male" || str == "Male")
                return 1;
            else
                if (str == "Female" || str == "female")
                    return 0;
            return -1;
        }
        public static int ToInt1(object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// function convert int to bool 
        /// </summary>
        /// <param name="number">number 0 or 1</param>
        /// <returns>true if number=1 </returns>
        public static bool ConverIntToBool(int number)
        {
            if (number == 1)
                return true;
            return false;
        }
        /// <summary>
        /// function convert bool to int 
        /// </summary>
        /// <param name="number">bol is true or false</param>
        /// <returns>1 if bol=true .. </returns>
        public static string ConverIntToString(int number)
        {
            if (number == 1)
                return "true";
            return "false";
        }

        public static string ConvertStringToString(string bol)
        {
            if (bol == "true")
                return "false";
            else
                if (bol == "false")
                    return "true";
            return "";
        }
        public static float ToFloat(object obj)
        {
            try
            {
                float rs = float.Parse(obj.ToString());
                if (rs < 0) return 0;
                return rs;
            }
            catch
            {
                return 0;
            }
        }
        public static string ToString(object obj)
        {
            try
            {
                return Convert.ToString(obj);
            }
            catch
            {
                return "";
            }
        }
        public static DateTime ToDateTime(object obj)
        {
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch
            {
                return Convert.ToDateTime("1/1/2009");
            }
        }


        public static string EncodePassword(string strPassword, string strFormat)
        {
            if (strFormat == string.Empty)
            {
                strFormat = "MD5";
            }
            return FormsAuthentication.HashPasswordForStoringInConfigFile(strPassword, strFormat);
        }
        public static string EncodePassword(string pass)
        {
            Byte[] original;
            Byte[] encoder;
            MD5 md5 = new MD5CryptoServiceProvider();
            original = ASCIIEncoding.Default.GetBytes(pass);
            encoder = md5.ComputeHash(original);
            return BitConverter.ToString(encoder);
        }
        public static string RandomString(int length, bool upperString, bool Number)
        {
            Random AppRandom = new Random((int)DateTime.Now.Ticks);
            string str = "abcdefghijklmnopqrstuvwxyz";
            if (Number)
            {
                str = str + "0123456789";
            }
            if (upperString)
            {
                str = str + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                int r = AppRandom.Next(0, str.Length);
                sb.Append(str.Substring(r, 1));
            }
            return sb.ToString();
        }

    }
}

